package com.ruoyi.alarm.service.impl;

import java.util.Collections;
import java.util.Date;
import java.util.List;

import com.ruoyi.alarm.domain.AlarmType;
import com.ruoyi.alarm.service.IAlarmTypeService;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.push.PushService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.alarm.mapper.AlarmWarningMapper;
import com.ruoyi.alarm.domain.AlarmWarning;
import com.ruoyi.alarm.service.IAlarmWarningService;

/**
 * 告警记录Service业务层处理
 * 
 * @author yepanpan
 * @date 2025-03-13
 */
@Service
public class AlarmWarningServiceImpl implements IAlarmWarningService 
{
    private static final Logger log = LoggerFactory.getLogger(AlarmWarningServiceImpl.class);

    @Autowired
    private AlarmWarningMapper alarmWarningMapper;
    @Autowired
    private IAlarmTypeService alarmTypeService;
    @Autowired
    private PushService pushService;

    /**
     * 查询告警记录
     * 
     * @param id 告警记录主键
     * @return 告警记录
     */
    @Override
    public AlarmWarning selectAlarmWarningById(Long id)
    {
        AlarmWarning alarmWarning = alarmWarningMapper.selectAlarmWarningById(id);
        if(alarmWarning != null){
            fillInfo(Collections.singletonList(alarmWarning));
        }
        return alarmWarning;
    }

    /**
     * 查询告警记录列表
     * 
     * @param alarmWarning 告警记录
     * @return 告警记录
     */
    @Override
    public List<AlarmWarning> selectAlarmWarningList(AlarmWarning alarmWarning)
    {
        List<AlarmWarning> list = alarmWarningMapper.selectAlarmWarningList(alarmWarning);
        if(list != null && !list.isEmpty()){
            fillInfo(list);
        }
        return list;
    }

    /**
     * 填充详细信息
     * @param list
     */
    private void fillInfo(List<AlarmWarning> list){
        for(AlarmWarning alarmWarning:list) {
            AlarmType alarmType = alarmTypeService.selectAlarmType(alarmWarning.getAlarmType());
            if(alarmType != null){
                alarmWarning.setType(alarmType);
            }
        }
    }

    /**
     * 新增告警记录
     * 
     * @param alarmWarning 告警记录
     * @return 结果
     */
    @Override
    public int insertAlarmWarning(AlarmWarning alarmWarning)
    {
        if(alarmWarning.getAlarmTime() == null){
            alarmWarning.setAlarmTime(new Date());
        }
        int ret = alarmWarningMapper.insertAlarmWarning(alarmWarning);
        //推送告警
        pushService.pushAlarm(alarmWarning);
        return ret;
    }

    /**
     * 修改告警记录
     * 
     * @param alarmWarning 告警记录
     * @return 结果
     */
    @Override
    public int updateAlarmWarning(AlarmWarning alarmWarning)
    {
        return alarmWarningMapper.updateAlarmWarning(alarmWarning);
    }

    /**
     * 批量删除告警记录
     * 
     * @param ids 需要删除的告警记录主键
     * @return 结果
     */
    @Override
    public int deleteAlarmWarningByIds(Long[] ids)
    {
        return alarmWarningMapper.deleteAlarmWarningByIds(ids);
    }

    /**
     * 删除告警记录信息
     * 
     * @param id 告警记录主键
     * @return 结果
     */
    @Override
    public int deleteAlarmWarningById(Long id)
    {
        return alarmWarningMapper.deleteAlarmWarningById(id);
    }

    /**
     * 处置告警
     *
     * @param alarmWarning 告警记录
     * @return 结果
     */
    @Override
    public int handleAlarmWarning(AlarmWarning alarmWarning){
        alarmWarning.setHandleStatus(AlarmWarning.HANDLE_DONE);
        AlarmWarning old = alarmWarningMapper.selectAlarmWarningById(alarmWarning.getId());
        if(old == null || old.getHandleStatus().equals(alarmWarning.getHandleStatus())){
            return 1;
        }

        SysUser user = SecurityUtils.getLoginUser().getUser();
        alarmWarning.setHandleUserName(user.getNickName());
        alarmWarning.setHandleUserId(user.getUserId());
        alarmWarning.setHandleTime(new Date());

        //处理当前设备所有未处置的同类告警
        AlarmWarning wc = new AlarmWarning();
        wc.setDeviceId(old.getDeviceId());
        wc.setAlarmType(old.getAlarmType());
        wc.setHandleStatus(AlarmWarning.HANDLE_UNDO);
        List<AlarmWarning> list = alarmWarningMapper.selectAlarmWarningList(wc);
        for(AlarmWarning warning:list){
            warning.setHandleStatus(alarmWarning.getHandleStatus());
            warning.setHandleResult(alarmWarning.getHandleResult());
            warning.setHandleRemark(alarmWarning.getHandleRemark());
            warning.setHandleTime(alarmWarning.getHandleTime());
            warning.setHandleUserId(alarmWarning.getHandleUserId());
            warning.setHandleUserName(alarmWarning.getHandleUserName());
            alarmWarningMapper.updateAlarmWarning(warning);
            //推送处置
            pushService.pushHandle(warning);
        }
        return 1;
    }

    /**
     * 撤销告警
     *
     * @param deviceId
     * @param alarmType
     * @return 结果
     */
    @Override
    public int repealAlarmWarning(Long deviceId, String alarmType){
        return alarmWarningMapper.repealAlarmWarning(deviceId, alarmType);
    }
}
